package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.commons.net.nntp.Article;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.heima.common.constants.ArticleConstants.DEFAULT_TAG;

@Service
@RequiredArgsConstructor
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

private final ApArticleConfigMapper apArticleConfigMapper;
private final ApArticleContentMapper apArticleContentMapper;
private final ArticleFreemarkerService articleFreemarkerService;
    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;
//默认加载
    private final static Integer DEFAULT_PAGE_SIZE=10;
    private final ApArticleMapper apArticleMapper;

    /**
     * @param loadType 1:加载更多  2:加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadType, ArticleHomeDto dto) {

        //获取分页size，超过50按50算
        Integer size = dto.getSize();
        if(size==null||size==0)dto.setSize(DEFAULT_PAGE_SIZE);
        size=Math.min(size,MAX_PAGE_SIZE);

        //如果文章频道Id没设置，设置为默认频道id
        String tag = dto.getTag();
        if(tag==null)dto.setTag(DEFAULT_TAG);
        //如果最大和最小时间均没设置，(说明这是一个用户进入页面的默认加载)
        if(dto.getMaxBehotTime()==null)dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime()==null)dto.setMaxBehotTime(new Date());
//查询数据,返回文章集合
        //2.查询数据
        //对于文章，下架和删除的标识是存储在文章配置表的，因此需要多表查询
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadType);

        return ResponseResult.okResult(apArticles);


    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        if(dto==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //文章审核通过后，添加文章
        Long id = dto.getId();
        ApArticle article=new ApArticle();
        BeanUtils.copyProperties(dto,article);
        if(id==null){
            //说明是添加文章(添加文章，添加文章配置，添加文章内容)
            //添加文章
            save(article);
            //获取id
            id= article.getId();
            //添加文章配置
            ApArticleConfig apArticleConfig=new ApArticleConfig(id);
            apArticleConfigMapper.insert(apArticleConfig);
         //添加文章内容
            ApArticleContent apArticleContent=new ApArticleContent();
           apArticleContent.setArticleId(id);
           apArticleContent.setContent(dto.getContent());
apArticleContentMapper.insert(apArticleContent);
        }else{
            //说明是修改 修改文章，文章内容
            updateById(article);
            //根据文章id查询文章内容
            LambdaQueryWrapper<ApArticleContent> lam=new LambdaQueryWrapper<>();
            lam.eq(ApArticleContent::getArticleId,id);
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(lam);
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }
        //调用文章模板服务，将文章模板存储到minio
        articleFreemarkerService.buildArticleFreemarker(article,dto.getContent());
        return new ResponseResult().ok(id);
    }


}
